package cn.fb.preshop.controller;

import cn.fb.preshop.pojo.User;
import cn.fb.preshop.service.UserService;
import cn.fb.preshop.utils.ResultEntity;
import com.sun.org.apache.bcel.internal.classfile.Code;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.xml.transform.Result;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
/**
 * 用户控制器
 */
public class UserController {
    @Resource
    UserService userService;

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/user/login")
    public ResultEntity<User> Login(String username,String password) throws UnknownHostException {
        InetAddress address = InetAddress.getLocalHost();
        System.out.println("本机ip地址:"+address);
        User user=userService.getSelectUser(username,password);
            return ResultEntity.successWithoutData(user);
    }
    @RequestMapping("/user/register")
    public Map<String,Object> Register(String username, String password, String email){
        Map<String,Object> map=new HashMap<>();
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        //随机生成用户id
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer id=new StringBuffer();
        for(int i=0;i<8;i++){
            int number=random.nextInt(62);
            id.append(str.charAt(number));
        }
        user.setUserid(id.toString());
        if(userService.getInsertUser(user)==1){
           map.put("data",1);
           map.put("userid",id);
        }else{
            map.put("data",0);
        }
        return map;
    }

}
